package com.jdls.tanhua.dubbo.impl;

import com.jdls.tanhua.dubbo.VideoApi;
import com.jdls.tanhua.dubbo.utils.IdWorker;
import com.jdls.tanhua.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/11/6
 */
@DubboService
public class VideoImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void save(Video video) {
        video.setVid(idWorker.getNextId("video"));
        mongoTemplate.save(video);
    }

    @Override
    public List<Video> findAll(Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where(null)).with(Sort.by(Sort.Order.desc("created"))).skip((page-1)*pagesize).limit(pagesize);

        List<Video> videos = mongoTemplate.find(query, Video.class);
        return videos;
    }
}
